package com.twt.service;

import com.twt.Dao.MyLakeDao;
import com.twt.Dao.UserDao;
import com.twt.entity.Result;
import com.twt.entity.User;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;


@Service
@Transactional(rollbackFor = RuntimeException.class)
public class MyLakeService {
    @Autowired
    private MyLakeDao MY_LAKE_DAO;
    @Autowired
    private UserDao userMapper;
    /**
     * 用户自定义头像上传
     *
     * @param username,file 用户名和头像
     * @return Result
     */

    private String filePath="/home/twt/headPic/";
    public Result setHead(MultipartFile file, String username)
    {
        Result result = new Result();//初始化result
        result.setDetail(null);
        result.setSuccess(false);
        result.setMsg(null);

        File filee = new File(filePath);
        if (!filee.exists()) {
            filee.mkdirs();
        }
        //文件名称
        String realFileName = file.getOriginalFilename();
        //文件后缀
        String suffix = realFileName.substring(realFileName.lastIndexOf(".") + 1);
        /***************文件处理*********************/
        String newFileName = UUID.randomUUID() + realFileName;
        String newFilePath = filePath + newFileName;
        //新文件的路径
        try {
            float scale=0.5f;//缩放值
            float quality=0.5f;//缩放质量

            //小于100kb的不压缩
            long filesize=file.getSize();
            if(filesize<100000)
            {
                scale=1f;
                quality=1f;
            }
            else if(filesize<1000000)
            {
                scale=0.8f;
                quality=0.8f;
            }
            //将传来的文件压缩并写入新建的文件
            Thumbnails.of(file.getInputStream()).scale(scale).outputQuality(quality).toFile(new File(newFilePath));

            //写入数据库
            MY_LAKE_DAO.setHead(username, newFileName);
            result.setSuccess(true);
            result.setMsg("自定义头像设置完成");

        } catch (IllegalStateException | IOException e) {
            //处理异常
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    public Result changePassword(String username, String password, String newPassword1, String newPassword2) {
        Result result = new Result();
        if (newPassword1.equals(newPassword2)) {
            Long userId = userMapper.login(username, password);
            if (userId != null) {
                MY_LAKE_DAO.changePassword(username, newPassword1);
                result.setResult("修改成功", true);
            } else {
                result.setResult("原密码错误", false);
            }
        } else {
            result.setResult("两次密码不一致", false);
        }

        return result;
    }

    //计算数据表数据数量
    private Long countUserNumber() {
        return userMapper.countUserNumber();
    }

    public Result changeNickname(String username, String nickname) {
        Result result = new Result();
        if (MY_LAKE_DAO.findNickname(nickname) != null) {
            result.setMsg("有相同昵称了");
            result.setSuccess(false);
        } else {
            MY_LAKE_DAO.changeNicknameInUser(username, nickname);
            MY_LAKE_DAO.changeNicknameInPicked(username, nickname);
            MY_LAKE_DAO.changeNicknameInThrowed(username, nickname);
            MY_LAKE_DAO.changeNicknameInLake(username,nickname);
            result.setMsg("昵称修改成功");
            result.setSuccess(true);
        }
        return result;
    }

    public Result shutDown(String username) {
        Result result = new Result();
        MY_LAKE_DAO.setUserNonexistentInPicked(username);
        MY_LAKE_DAO.setUserNonexistentInThrowed(username);
        MY_LAKE_DAO.deleteLakeWhoNonexistent(username);
        MY_LAKE_DAO.deleteUserWhoNonexistent(username);
        result.setResult("注销成功", true);
        return result;
    }


}

